// pages/customizeActivity/companyCustomize/companyCustomize.js
var util = require('../../../utils/util.js');
var wxcache = require('../../../utils/wxcache.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    max:100,
    currentWordNumber: '',
    username:'',
    phone:'',
    startDate:'',
    endDate: '',
    dateGap:null,
    showModal: false,
    modalData:null,
    agel: 12,
    ager: 60,

  },
  peopleNumRegex: /^[1-9]\d*$/,
  phoneRegex: /^1[34578]\d{9}$/,
  name:'',
  business:'',
  campaignType:[],
  male:'',
  famale:'',
  price:'',
  remark:'',
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that = this
    var userinfo = wxcache.get("userInfo")
    var phone = wxcache.get("phoneNumber")

    that.setData({
      username : userinfo.nickName,
      phone: phone
    })
    
    if (options && options.from_url == 'wake'){
      that.setData({
        showModal: true
      })
      that.getModel();
    } 



  },
  getModel:function(){

    var that = this;
    util.request(api.ConfRequest, { type: 'ENTERPRISE_CUSTOMIZATION_RULE' }).then(function (res) {
      if ('200' == res.code) {

        console.log(res)
        var result = res.responseBody[0]
        that.setData({
          modalData:result
        })
      }
    });

  },
  hideModal: function () {
    this.setData({
      showModal: false,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindstartDateChange(e) {
    console.log('startdate:', e.detail.value)

    var dateGap = this.dateMinus(e.detail.value, this.data.endDate)
    console.log(dateGap)
    if (dateGap<=0 && dateGap!=null){
      wx.showModal({
        content: '开始时间不能大于结束时间',
        showCancel:false
      })
      e.detail.value = ''
    }

    this.setData({
      startDate: e.detail.value,
      dateGap: dateGap
    })

  },
  bindendDateChange(e) {
    console.log('enddate:', e.detail.value)

    var dateGap = this.dateMinus(this.data.startDate,e.detail.value)
    console.log(dateGap)
    if (dateGap <= 0 && dateGap != null) {
      wx.showModal({
        content: '开始时间不能大于结束时间',
        showCancel: false
      })
      e.detail.value = ''
    }

    this.setData({
      endDate: e.detail.value,
      dateGap: dateGap
    })


  },
  checkboxChange(e) {
    console.log(e.detail.value)
    this.campaignType = e.detail.value
  },
  calcTextArea:function(e){
     
    // 获取输入框的内容
    var value = e.detail.value;

    // 获取输入框内容的长度
    var len = parseInt(value.length);
    // console.log('当前输入长度：' + len)

    //最多字数限制
    if (len > this.data.max) return;
    // 当输入框内容的长度大于最大长度限制（max)时，终止setData()的执行
    this.setData({
      currentWordNumber: len //当前字数  
    });
    
    this.data.remark = value

  },
  switchTab:function(e){
    wx.redirectTo({
      url: '/pages/customizeActivity/myCustomize/myCustomize',
    })
  },
  dateMinus:function (sDate,eDate){
    if(!sDate || !eDate){
      return null;
    }
    var sdate = new Date(sDate.replace(/-/g, "/"));
    var eDate = new Date(eDate.replace(/-/g, "/"));
    var days = eDate.getTime() - sdate.getTime();
    var day = parseInt(days / (1000 * 60 * 60 * 24));
    return day+1;
  },

  submitCustomize:function(e){

    var that = this;
    var user_id = wxcache.get("userInfo").id;
    var param = {}
    param.name = that.name
    param.business = that.business
    param.typeList = that.campaignType
    param.menWomenNumList = [that.male,that.famale]
    param.ageGroup = [that.data.agel,that.data.ager]
    param.campaignBeginStart=that.data.startDate
    param.campaignBeginEnd = that.data.endDate
    param.budget = that.price
    param.contact = that.data.username
    param.contactMobile= that.data.phone
    param.remark= that.data.remark

    console.log('请求参数：',param)

    if(!that.checkParam(param)){

      return;
    }
    
    util.request(api.createEnterpriseCustomization,param,'POST').then(res=>{

        console.log(res)

        if(res.code == 200){

            wx.showModal({
              // title: '提示',
              content: '定制申请提交成功',
              showCancel:false,
              confirmColor: '#F4881C',
              success: function (res) {
                if (res.confirm) {
                   wx.redirectTo({
                     url: '/pages/customizeActivity/myCustomize/myCustomize',
                   })
                }
              }
            })
        }else{
          wx.showModal({
            content: res.responseBody,
            showCancel:false
          })
        }
    })

  },
  checkParam:function(data){
      
      if(!data.name){
        wx.showModal({
          content: '企业名称不能为空',
          showCancel:false
        })
        return false;
      }

    if (!data.campaignBeginStart || !data.campaignBeginEnd){
        wx.showModal({
          content: '活动日期输入有误',
          showCancel: false
        })
        return false;
      }

    if (!this.famale || !this.male) {
      wx.showModal({
        content: '男女人数输入有误',
        showCancel: false
      })
      return false;
    }

    if (!this.data.agel || !this.data.ager) {
      wx.showModal({
        content: '年龄区间输入有误',
        showCancel: false
      })
      return false;
    }
    if (!data.budget) {
      wx.showModal({
        content: '活动预算不能为空',
        showCancel: false
      })
      return false;
    }

    if (!data.contact) {
      wx.showModal({
        content: '联系人不能为空',
        showCancel: false
      })
      return false;
    }

    if (!data.contactMobile) {
      wx.showModal({
        content: '联系方式输入有误',
        showCancel: false
      })
      return false;
    }


    return true;



  },


  //bind inputs
  nameInput:function(e){

    this.name= e.detail.value;

  },
  businessInput:function(e){
    this.business = e.detail.value;
  },
  maleInput:function(e){
    
    var male = e.detail.value;
    var regex = this.peopleNumRegex

    if (!male){
      return;
    }

    var flag = regex.test(male)
    if (!flag) {
        wx.showModal({
          content: '输入有误，请重新输入',
          showCancel: false
        })
    }else{
      this.male = male
    }
 
  },
  famaleInput:function(e){

    var famale = e.detail.value;
    var regex = this.peopleNumRegex

    if (!famale) {
      return;
    }
    var flag = regex.test(famale)
    if (!flag) {
      wx.showModal({
        content: '输入有误，请重新输入',
        showCancel: false
      })
    }else{
      this.famale = famale
    }
  },
  agelInput:function(e){

    var agel = e.detail.value;
    var regex = this.peopleNumRegex

    if (!agel) {
      return;
    }
    var flag = regex.test(agel)
    if (!flag) {
      wx.showModal({
        content: '输入有误，请重新输入',
        showCancel: false
      })
    } else {
      this.data.agel = agel
    }
  },
  agerInput:function(e){

    var ager = e.detail.value;
    var regex = this.peopleNumRegex

    if (!ager) {
      return;
    }
    var flag = regex.test(ager)
    if (!flag) {
      wx.showModal({
        content: '输入有误，请重新输入',
        showCancel: false
      })
    } else {
      this.data.ager = ager
    }
  },
  priceInput:function(e){

    var price = e.detail.value;
    var regex = this.peopleNumRegex

    if (!price) {
      return;
    }
    var flag = regex.test(price)
    if (!flag) {
      wx.showModal({
        content: '输入有误，请重新输入',
        showCancel: false
      })
    } else {
      this.price = price
    }
  },
  usernameInput:function(e){
    var username = e.detail.value;
    this.data.username = username
    
  },
  phoneInput:function(e){
    var phone = e.detail.value;
    var regex = this.phoneRegex

    if (!phone) {
      return;
    }
    var flag = regex.test(phone)
    if (!flag) {
      wx.showModal({
        content: '输入有误，请重新输入',
        showCancel: false
      })
    } else {
      this.data.phone = phone
    }
  } 


})